package com.community.common.controller;


import com.community.common.pojo.PageRequest;
import com.community.common.pojo.dto.VisitorApplyDto;
import com.community.common.pojo.dto.VisitorCondition;
import com.community.common.pojo.dto.VisitorReceiveDto;
import com.community.common.pojo.response.LayuiPage;
import com.community.common.pojo.response.MyResponse;
import com.community.common.pojo.vo.VisitorVo;
import com.community.common.service.ManagerUserService;
import com.community.common.service.VisitorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * 访客记录表 前端控制器
 * </p>
 *
 * @author superman
 * @since 2020-08-02
 */
@Slf4j
@Controller
@RequestMapping("/visitor")
public class VisitorController {

    @Resource
    private VisitorService visitorService;
    @Resource
    private ManagerUserService managerUserService;

    @ResponseBody
    @PostMapping("/queryByPage")
    public LayuiPage queryByPage(@RequestBody PageRequest<VisitorCondition> visitorConditionPageRequest, HttpSession httpSession) {
        List<VisitorVo> visitors = visitorService.queryByPage(visitorConditionPageRequest,
                managerUserService.getCommunityIdFromSession(httpSession));
        return LayuiPage.parsePage(visitors);
    }

    /**
     * @return 访客发起拜访申请
     */
    @ResponseBody
    @PostMapping("/applyVisit")
    public MyResponse applyVisit(@RequestBody VisitorApplyDto visitorApplyDto) {
        log.info("applyVisit 参数：{}", visitorApplyDto);
        boolean res = visitorService.applyVisit(visitorApplyDto);
        if (res) {
            return MyResponse.success();
        }
        return MyResponse.fail();
    }

    /**
     * @return 业主同意拜访申请
     */
    @ResponseBody
    @PostMapping("/receiveVisit")
    public MyResponse receiveVisit(@RequestBody VisitorReceiveDto visitorReceiveDto) {
        return visitorService.receiveVisit(visitorReceiveDto);
    }

    /**
     * @return 业主的被拜访历史
     */
    @ResponseBody
    @PostMapping("/ownerVisitedHistory")
    public LayuiPage ownerVisitedHistory(@RequestBody PageRequest<VisitorApplyDto> pageRequest) {
        List<VisitorVo> visitors = visitorService.ownerVisitedHistory(pageRequest);
        return LayuiPage.parsePage200(visitors);
    }

    /**
     * @return 业主的待审核列表
     */
    @ResponseBody
    @PostMapping("/ownerCheckPending")
    public LayuiPage ownerCheckPending(@RequestBody PageRequest<VisitorApplyDto> pageRequest) {
        List<VisitorVo> visitors = visitorService.ownerCheckPending(pageRequest);
        return LayuiPage.parsePage200(visitors);
    }

    /**
     * @return 访客的拜访历史
     */
    @ResponseBody
    @PostMapping("/visitorVisitedList")
    public LayuiPage visitorVisitedList(@RequestBody PageRequest<VisitorApplyDto> pageRequest) {
        List<VisitorVo> visitors = visitorService.visitorVisitedList(pageRequest);
        return LayuiPage.parsePage200(visitors);
    }

}
